/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vn.ac.aptech.fat1.c0910g.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import vn.ac.aptech.fat1.c0910g.dao.FoodDAO;
import vn.ac.aptech.fat1.c0910g.entity.Food;

/**
 *
 * @author NDC
 */
public class FoodDaoImpl implements FoodDAO {
    private Connection conn;

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public FoodDaoImpl(){

    }

    public FoodDaoImpl(Connection con){
        this.conn = con;
    }

    public boolean addFood(Food f) throws Exception {
        try {
            PreparedStatement stm = conn.prepareStatement("INSERT INTO Food VALUES(?,?,?,?)");
            stm.setString(1, f.getName());
            stm.setFloat(2, f.getPrice());
            stm.setString(3, f.getDescription());
            stm.setString(4, f.getLinkImage());

            int k = stm.executeUpdate();
            if(k>0){
                return true;
            }
            return false;
        } catch (Exception e) {
            throw e;
        }
    }

    public boolean updateFood(Food f) throws Exception {
        try {
            PreparedStatement stm = conn.prepareStatement("UPDATE Food SET name=?, price=?,description=?, linkImage=? where foodId=?");
            stm.setString(1, f.getName());
            stm.setFloat(2, f.getPrice());
            stm.setString(3, f.getDescription());
            stm.setString(4, f.getLinkImage());
            stm.setInt(5, f.getId());
            int k = stm.executeUpdate();
            if(k>0){
                return true;
            }
            return false;
        } catch (Exception e) {
            throw e;
        }
    }

    public boolean deleteFood(Food f) throws Exception {
        try {
            PreparedStatement stm = conn.prepareStatement("DELETE FROM Food where foodId=?");
        
            stm.setInt(1, f.getId());

            int k = stm.executeUpdate();
            if(k>0){
                return true;
            }
            return false;
        } catch (Exception e) {
            throw e;
        }
    }

    public Food getFoodById(int fid) throws Exception {
        try {
            PreparedStatement stm = conn.prepareStatement("SELECT * FROM Food WHERE foodId=?");
            stm.setInt(1, fid);

            ResultSet rs = stm.executeQuery();
            if (rs.next()){
                Food f = new Food();
                f.setId(rs.getInt("foodId"));
                f.setName(rs.getString("name"));
                f.setPrice(rs.getFloat("price"));
                f.setDescription(rs.getString("description"));
                f.setLinkImage(rs.getString("linkImage"));

                return f;
            }
            return null;
        } catch (Exception e) {
            throw e;
        }
    }

    public ArrayList<Food> listFoods() throws Exception {
        try {
            ArrayList<Food> listFood = new ArrayList<Food>();
            PreparedStatement stm = conn.prepareStatement("SELECT * FROM Food");
            ResultSet rs = stm.executeQuery();

            while(rs.next()){
                Food f = new Food();
                f.setId(rs.getInt("foodId"));
                f.setName(rs.getString("name"));
                f.setPrice(rs.getFloat("price"));
                f.setDescription(rs.getString("description"));
                f.setLinkImage(rs.getString("linkImage"));
                listFood.add(f);
            }
            return listFood;
        } catch (Exception e) {
            throw e;
        }
    }
}
